<?
/**
 * VoiceOne Settings/Meetme/edit
 *
 * Frontend to provide access to the meetme system in voiceone.
 * @package voiceone
 * @subpackage  settings_meetme
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  require_once("$absBasePath/lib/xajax/xajax.inc.php"); 
  sajax_init();
  
  //================ XAJAX ================
  function processFormData($formData){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    //extract form params
    $id=$formData["id"];
    $confno=$formData["confno"];
    $pin=$formData["pin"];
    $adminpin=$formData["adminpin"];
    $app_id=$formData["app_id"];
    $imgErr="<img src=\"".$relBasePath."/public/img/shutdown.png\" alt=\"error\">";
    //get all extensions
    $ret=$asterisk->getExtension(0);
    if(is_array($ret)){
      foreach($ret as $e){
        if(($app_id!=0)&&($app_id!="")&&($e["entity_id"]==$app_id)) continue;
        else $extenArr[]=$e["extension"];
      }
    }  
    // Check Conference Number ((no null)&&(numeric)&&(unique)&&( -neq Alias))
    $confnoErr=false;
    if($confno==""){ 
      //room cannot be null
      $errOut[]= T_("Room number cannot be null");
      $objResponse->addAssign("confnoErr","innerHTML",$imgErr);
      $confnoErr=true;
    }else if(!is_numeric($confno)){
      //room must be numeric
      $errOut[]= T_("Room number allow only numeric characters. Please change it");
      $objResponse->addAssign("confnoErr","innerHTML",$imgErr);
      $confnoErr=true;
    }else if((is_array($extenArr))&&(in_array($confno,$extenArr))){  
      $errOut[]= T_("Room number already in use. Please change it");
      $objResponse->addAssign("confnoErr","innerHTML",$imgErr);
      $confnoErr=true;
    }
    if($confnoErr===false) $objResponse->addAssign("confnoErr","innerHTML","");
    // Check pin ((null)||((no null)&&(numeric)))
    $pinErr=false;
    if(($pin!="")&&(!is_numeric($pin))){ 
      //room must be numeric
      $errOut[]= T_("Pin number allow only numeric characters. Please change it");
      $objResponse->addAssign("pinErr","innerHTML",$imgErr);
      $pinErr=true;
    }
    if($pinErr===false) $objResponse->addAssign("pinErr","innerHTML","");
    // Check admin pin ((null)||((no null)&&(numeric)))
    $adminpinErr=false;
    if(($adminpin!="")&&(!is_numeric($adminpin))){ 
      //room must be numeric
      $errOut[]= T_("Admin pin number allow only numeric characters. Please change it");
      $objResponse->addAssign("adminpinErr","innerHTML",$imgErr);
      $adminpinErr=true;
    }
    if($adminpinErr===false) $objResponse->addAssign("adminpinErr","innerHTML","");
    // Process Form 
    if(isset($errOut)){
      $error="<div class=\"AlertBad\"><ul>";
      foreach($errOut as $err) $error.="<li>".$err."</li>";
      $error.="</ul></div>";
      $objResponse->addAssign("alert","innerHTML",$error);
      $objResponse->addAssign("loading","innerHTML","");
    }else{
      $objResponse->addAssign("loading","innerHTML",""); 
      $objResponse->addScript(" document.getElementById('mainform').submit(); ");
    }
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
    
  $xajax = new xajax();
  $xajax->registerFunction("processFormData");
  $xajax->processRequests();
  
  //================== POST ==================
  if (count($_POST)>0) {
    // create the entry array with the posted data  
    $entry=Array(
      "id"        => $_POST["id"],
      "confno"    => $_POST["confno"],
      "pin"       => $_POST["pin"],
      "adminpin"  => $_POST["adminpin"],
      "options"   => $_POST["options"]
    );     
    $entry["application"]["groups"] = $_POST["groups"];    
    // put room
    $ret=$asterisk->putMeetme($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".T_("The conference room has been modified")."</div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".T_("The conference room hasn't been modified")."</div>";
    goToMainPage($alert);
  }

  //================ PAGE LOGIC ================
  // retrieve the meetme informations
  if ($_GET["id"] != "") {
    $meetme   = $asterisk->getMeetme($_GET["id"]);
    $id       = $meetme[0]["id"]; 
    $confno   = $meetme[0]["confno"];
    $pin      = $meetme[0]["pin"];
    $adminpin = $meetme[0]["adminpin"];
    $app_id   = $meetme[0]["app_id"];
    $app_name = $meetme[0]["app_name"];
    $options  = $meetme[0]["options"];
    $parameter= $meetme[0]["application"][0]["parameter"];
    $groups   = $meetme[0]["application"][0]["groups"];
    $title    = sprintf(T_("Modify Conference Room %1\$s"),$confno );     
  } else {
    $confno   = "";
    $pin      = "";
    $adminpin = "";
    $title    = T_("New Conference Room");
  }
  
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../admin/lib/xajax/"); 
  ?>
  <div class="SectionHeader">
    <h1><?=$title?></h1>
    <h3>
      <a href="../" class="admin" ><?=T_("Back without saving")?></a>
    </h3>
  </div>
  <div id="alert"></div>
  <div id="basic_form">
    <form id="mainform" action="<?=$PHP_SELF?>" method="post">
      
      <div id="client_form" >
         <h2><?=T_("Conference Room")?></h2>  
        <p class="blockintro"><?=T_("Define the conference room options")?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="confno"><?=T_("Room No :")?></label></dt> 
            <dd><input type="text" size="25" id="confno" name="confno" value="<?=$confno?>" /><span id="confnoErr"></span> <span class="help"><?=T_("Mandatory field, must be numeric like 8000")?></span></dd>
          </dl>
          <dl>
            <dt class="required"><label for="pin"><?=T_("User Pin")?>:</label></dt>
            <dd><input type="text" size="25" id="pin" name="pin" value="<?=$pin?>" /><span id="pinErr"></span> <span class="help"><?=T_("Optional field, must be numeric like 1234")?></span></dd>
          </dl>
          <dl>
            <dt class="required"><label for="adminpin"><?=T_("Admin Pin")?>:</label></dt>
            <dd><input type="text" size="25" id="adminpin" name="adminpin" value="<?=$adminpin?>" /><span id="adminpinErr"></span> <span class="help"><?=T_("Optional field, must be numeric like 8818")?></span></dd>
          </dl>
          <dl>
            <dt class="required"><label for="adminpin"><?=T_("Options:")?></label></dt>
            <dd><input type="text" size="25" id="options" name="options" value="<?=$options?>" /> <span class="help"><?=T_("Advanced options")?></span></dd>
          </dl>
        </div>
        <input type="hidden" name="id" value="<?=$id?>" />
        <input type="hidden" name="app_id" value="<?=$app_id?>" />
      </div>            

      <?echo listGroup(GROUP_OF_APPLICATION, $js, $groups);?>      
      
      <div class="action">
        <a href="#"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/></a>
        <a href="#"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" onclick=" document.getElementById('alert').innerHTML=''; xajax_processFormData(xajax.getFormValues('mainform')); "/></a>
      <!--
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Annulla" onclick="location.href='../'; return false;"/>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Salva" />
       -->
      </div>
    </form>
  </div>  
  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=T_("Add or update a Conference Room")?></h1>
          <?=T_("<p>It is possible to create conference rooms with or without pin numbers</p>")?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
 	sajax_handle_client_request();

  $smarty->assign("section", "meetme");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->assign("ajax", $js);
  $smarty->display('settings.tpl');

?>
